const CacheTableItem = require("./models/cacheTableItem.js")

let __cacheTable = {};

/** 
 * 注册缓存数据表 
 */
function registerModule(cacheConfig) {
	cacheConfig.forEach(configItem => {
		let cacheKey = configItem.key;
		let cacheItem = new CacheTableItem(configItem);
		cacheItem.initData();
		__cacheTable[cacheKey] = cacheItem;
	})
}

/**
 * 更新缓存数据
 * @param {String} key 缓存key
 * @param {Any} data 更新数据
 */
function updateCacheData(key, data) {
	__cacheTable[key].updateCacheData(data);
}

/**
 * 读取缓存数据
 * @param {String} key 缓存key
 */
function readCacheData(key) {
	let result = __cacheTable[key].readCacheData();
	return result;
}

/**
 * 导出缓存表数据 用以调试
 * @returns {Object} 缓存表数据
 */
function exportCacheTable() {
	return __cacheTable;
}

module.exports = {
	registerModule,

	updateCacheData,
	readCacheData,
	exportCacheTable,
}